package graphEd.saisie;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import table.EditorDefaultTableModel;
import table.EditorTable;
import DataStorage.InfluenceDAOHibernate;
import HibernateSQLite.java.model.Influence;

import com.mxgraph.util.mxResources;

public class EditorInfluences extends JPanel {
	private static final long serialVersionUID = 1L;
	private EditorTable table = new EditorTable(null, 3);
	private List<Long> oldInfluence_id = new ArrayList<Long>();

	public EditorInfluences() {
		this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		((EditorDefaultTableModel) this.table.getModel()).setRowUneditable(0);
		table.getModel().addRow(
				new String[] {mxResources.get("cuts"), mxResources.get("influencingOP1"), mxResources.get("influencingOP2")});
		table.getTable().setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

		this.add(new JScrollPane(table.getTable()));
	}
	
	public void create(long infoLigneId, List<String> listOpPrincipal){
		
		InfluenceDAOHibernate influenceDAO = new InfluenceDAOHibernate();
		List<String> listCoupes = table.getValuesInColumn(0, 1);
		List<String> listInfluence1 = table.getValuesInColumn(1,1);
		List<String> listInfluence2 = table.getValuesInColumn(2,1);
		//int count = 0;
		//for(int i=0; i<listCoupes.size(); i++){
			System.out.println("=========================" +
					"\ncoupe: "+ listCoupes.size() +
					"\nop principal: " + listOpPrincipal.size() +
					"\ninfluence 1: " + listInfluence1.size() +
					"\ninfluence 2: " + listInfluence2.size());
		//}
		
		
		for(Long l:oldInfluence_id){
			System.out.println("Suppression:"+l);
			influenceDAO.deleteById(l);
		}
		
		//influenceDAO.saveInfluence(listCoupes, listOpPrincipal, listInfluence1,
		//		listInfluence2, infoLigneId);
		
		influenceDAO.getSession();
		List<Influence> listInfluences = new ArrayList<Influence>();
		
		for(int i=0; i<listCoupes.size(); i++){
			Influence influence = new Influence();
			influence.setOP_principal(listOpPrincipal.get(i));
			influence.setCoupe(listCoupes.get(i));
			influence.setOP_influence_1(listInfluence1.get(i));
			influence.setOP_influence_2(listInfluence2.get(i));
			influence.setInformationLigne_id(infoLigneId);
			listInfluences.add(influence);
		}

		
		
		System.out.println("Sauvegarde id: "+infoLigneId);
		for(Influence influence:listInfluences){
			System.out.println(influence.getCoupe());
		}
		System.out.println("kkkk:"+listCoupes);
		
		if (oldInfluence_id.isEmpty()){
			for(Influence i:influenceDAO.getListInfluence(infoLigneId)){
				oldInfluence_id.add(i.getInfluence_id());
			}		
		}
		Collections.sort(listInfluences);
		influenceDAO.saveInfluence(listInfluences);
	}
	
	public void open(long infoLigneId){
		InfluenceDAOHibernate influenceDAO = new InfluenceDAOHibernate();

		List<Influence> listInfluence = influenceDAO.getListInfluence(infoLigneId);
		//Set<Influence> setInfluence = new HashSet<Influence>(listInfluence);
		
		Collections.sort(listInfluence);
		
		int i=1;
		
		for(Influence inf:listInfluence){
			System.out.println("Classement:"+inf.getCoupe());
			oldInfluence_id.add(inf.getInfluence_id());
			//table.setValueAt(inf.getCoupe(), i, 0);
			table.setValueAt(inf.getOP_influence_1(), i, 1);
			table.setValueAt(inf.getOP_influence_2(), i++, 2);
		}
	}

	public void updateCoupesList(List<String> coupesList) {
		List<String> list = new ArrayList<String>();
		int i = 1;
		String[] replace = null;
		
		for (String s : coupesList) {
			if(s!=null){
				if(!s.contains(",")){
					list.add(s);
				}else{
					replace = s.split(",");
					for(String r:replace)
						list.add(r);
				}
			}
		}

		for (String s : list) {			
			//System.out.println("list.size():"+list.size()+"\ttable.getRowCount():"+table.getRowCount());
			while ((table.getRowCount()) <= (list.size()))
				table.addRow(new Object[]{ "" , "", ""});

			while ((table.getRowCount() - 1) > (list.size()))
				table.removeRow(table.getRowCount() - 1);
			
			System.out.println("setValueAt:"+s+"\t"+i);
			/*String opInfluente1 = table.getValueAt(i, 1);
			String opInfluente2 = table.getValueAt(i, 2);

			table.setValueAt(opInfluente1, i, 1);
			table.setValueAt(opInfluente2, i, 2);*/
			table.setValueAt(s.trim(), i++, 0);
		}

		//System.out.println("-----------Influences-----------------");
	}
}
